<template>
    <div class="body">
        <van-address-list
        v-model="chosenAddressId"
        @select="onChangeAddress"
        :list="list"
        default-tag-text="默认"
        tel="phone"
        @add="onSet"
        @edit="onSet"
        />
    </div>
</template>
<script>
import {Toast,Notify} from 'vant'
import {AddressList} from '../ajax'
export default {
    data() {
        return {
            chosenAddressId: 1,
            list: [],
        }
    },
    created(){
        this.getAddressList();
        console.log(this.$store.getters.wxUserInfo);
        this.chosenAddressId = this.$store.getters.wxUserInfo.company_address?this.$store.getters.wxUserInfo.company_address.id:null;
    },
    methods:{
        getAddressList(){
            const data = {
                group_id:this.$store.getters.wxUserInfo.group_id,
                admin_id:this.$store.getters.admin_id
            }
            AddressList(data).then(res=>{
                // console.log(res);
                for(let i of res){
                    i.address = i.province+i.city+i.area+i.dedail;
                    i.address1 = i.province+i.city+i.area;
                    i.isDefault = i.is_default == "1"?true:false;
                }
                console.log(res);
                this.list = res;
            })
        },
        onSet(item) {
            if(this.list.length>0 && !item){
                Notify({ type: 'danger', message: '已存在一个店铺地址' });
                return;
            }
            if(!item) item = {};
            this.$store.commit('set_address',item);
            this.$router.push('/addressEdit')
        },
        onChangeAddress(item){
            console.log(item);
            this.$store.commit('address',item);
        }
    }
}
</script>
<style lang="scss" scoped>
    $green:#3DCEB9;
    .body{
        height: calc(100vh - 6rem);
        background: #F2F2F2;
        overflow: auto;
        .van-address-item .van-radio__icon--checked .van-icon{
            background-color: $green;
            border-color:$green
        }
    }
    .van-button--danger{
        background-color:$green;
        border-color: $green;
    }
</style>